import Vue from 'vue'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import locale from 'element-ui/lib/locale/lang/en' // lang i18n
import zhLocale from 'element-ui/lib/locale/lang/zh-CN' // lang i18n
import '@/styles/index.scss' // global css
import App from './App'
import store from './store'
import router from './router'

import '@/icons' // icon
import '@/permission' // permission control
import {upload} from "./utils/upload";
import VueCropper from 'vue-cropper'
import echarts from 'echarts'

Vue.prototype.$echarts = echarts

Vue.prototype.hasPerm = (permission) => {
  let myPermissions = JSON.parse(localStorage.getItem("menus"));
  return myPermissions.indexOf(permission) > -1;
}

Vue.prototype.$upload = upload;

Vue.prototype.$normalMaxLimit = 250;

Vue.use(VueCropper)
import JsonExcel from 'vue-json-excel'

Vue.component('JsonExcel', JsonExcel)
Vue.prototype.$bigNumber = (type, n1, n2) => {
  let res;
  switch (type) {
    case "plus":
      n1 = new Big(n1);
      res = n1.plus(n2);
      break;
    case "minus":
      n1 = new Big(n1);
      res = n1.minus(n2);
      break;
    case "times":
      n1 = new Big(n1);
      res = n1.times(n2);
      break;
    case "div":
      n1 = new Big(n1);
      res = n1.div(n2);
      break;
    default:
      break;
  }
  return Number(res);
};
Date.prototype.Format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

Vue.filter('numberToAllDate', function (val) {
  let date = new Date(val);
  let dateStr = date.getFullYear() + '/' + ((date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + '/' + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '-' +
    (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':' + (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  return dateStr;
})
// format金额
Vue.filter('parseMoneyExcept', function (val) {
  if (val === "") {
    return;
  }
  val = new Big(val);
  let money = (val.div(100)).toFixed(2);
  return money;
})
Vue.filter('parseClassTime', function (val) {
  if (!val) {
    return;
  }
  let hours = Math.floor(val / (1000 * 60 * 60));
  let minutes = Math.floor((val % (1000 * 60 * 60)) / (60 * 1000));
  let seconds = Math.floor(((val % (1000 * 60 * 60)) % (60 * 1000)) / 1000);
  return `${hours}时${minutes}分${seconds}秒`;
})
Vue.filter('parseTime', function (val, cFormat) {
  if (!val) {
    return;
  }
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof val === 'object') {
    date = val
  } else {
    if ((typeof val === 'string') && (/^[0-9]+$/.test(val))) {
      val = parseInt(val)
    }
    if ((typeof val === 'number') && (val.toString().length > 10)) {
      val = val
    }
    date = new Date(val)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
})

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online ! ! !
 */
if (process.env.NODE_ENV === 'production') {
  const {mockXHR} = require('../mock')
  mockXHR()
}

// set ElementUI lang to EN
// Vue.use(ElementUI, {locale})
// 如果想要中文版 element-ui，按如下方式声明
Vue.use(ElementUI, {zhLocale})

Vue.config.productionTip = false

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
